package com.study.chapter06;

import com.study.entity.Event;
import com.study.chapter05.source.ClickSource;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

import java.util.HashSet;

/**
 * @Description:
 * @Author: LiuQun
 * @Date: 2022/8/3 21:24
 */
public class PvAndUvExampleTest {
    public static void main(String[] args) throws Exception {
        //环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<Event> stream = env.addSource(new ClickSource());

        //计算(PV / UV)的值，获得平均用户访问数

        //定义水位线
        SingleOutputStreamOperator<Event> watermarkOpt = stream.assignTimestampsAndWatermarks(
                WatermarkStrategy.<Event>forMonotonousTimestamps()
                        .withTimestampAssigner(new SerializableTimestampAssigner<Event>() {
                            @Override
                            public long extractTimestamp(Event element, long recordTimestamp) {
                                return element.timestamp;
                            }
                        })
        );
        //定义窗口
        SingleOutputStreamOperator<Double> resultOpt = watermarkOpt.keyBy(data -> true) //将所有数据设置相同的key，并发送到同一个分区
                .window(SlidingEventTimeWindows.of(Time.seconds(10), Time.seconds(2)))
                .aggregate(new CustomAvgPv());


        resultOpt.print();

        env.execute();
    }

    /**
     * 聚合函数
     */
    private static class CustomAvgPv implements AggregateFunction<Event, Tuple2<HashSet<String>,Long>,Double>{

        @Override
        public Tuple2<HashSet<String>, Long> createAccumulator() {
            //创建累加器，只会初始化一次
            return Tuple2.of(new HashSet<>(),0L);
        }

        @Override
        public Tuple2<HashSet<String>, Long> add(Event value, Tuple2<HashSet<String>, Long> accumulator) {
            //将用户添加到HashSet中
            accumulator.f0.add(value.user);
            //将数量加1，并返回一个累加器
            return Tuple2.of(accumulator.f0,accumulator.f1 + 1);
        }

        @Override
        public Double getResult(Tuple2<HashSet<String>, Long> accumulator) {
            // 窗口闭合时，增量聚合结束，将计算结果发送到下游
            Long pvNum = accumulator.f1; //PV的数量
            int userNum = accumulator.f0.size(); //用户的数量，即UV的数量
            return (double) pvNum / userNum;
        }

        @Override
        public Tuple2<HashSet<String>, Long> merge(Tuple2<HashSet<String>, Long> a, Tuple2<HashSet<String>, Long> b) {
            return null;
        }
    }
}
